document.addEventListener('DOMContentLoaded', function() {
    const saveButton = document.getElementById('save-intentions');

    if (saveButton) {
        saveButton.addEventListener('click', function(e) {
            e.preventDefault(); // 阻止默认提交行为

            // 获取表单元素
            const form = document.getElementById('job-intentions-form');

            // 使用FormData收集表单数据
            const formData = new FormData(form);

            // 使用fetch API提交表单
            fetch(form.action || window.location.href, {
                method: 'POST',
                body: formData,
                headers: {
                    'X-Requested-With': 'XMLHttpRequest'
                }
            })
            .then(response => response.json())
            .then(data => {
                if (data.code === 200) {
                    // 保存成功
                    Swal.fire({
                        title: '成功',
                        text: data.message || '简历信息保存成功',
                        icon: 'success',
                        confirmButtonText: '确定',
                        confirmButtonColor: '#1e3c72'
                    });
                } else {
                    // 保存失败
                    Swal.fire({
                        title: '错误',
                        text: data.message || '简历信息保存失败',
                        icon: 'error',
                        confirmButtonText: '确定',
                        confirmButtonColor: '#1e3c72'
                    });
                }
            })
            .catch(error => {
                console.error('Error:', error);
                Swal.fire({
                    title: '错误',
                    text: '提交过程中发生错误',
                    icon: 'error',
                    confirmButtonText: '确定',
                    confirmButtonColor: '#1e3c72'
                });
            });
        });
    }
});